// 封装异步函数，使用async + await，需要接收一个函数作为参数
const promisic = function(func){
	// 返回一个函数，这个函数接收一个对象作为参数
	return function(params = {}){
		// 返回一个Promise对象
		return new Promise((resolve,reject)=>{
			// 将params与另一个对象拼接后深拷贝赋值
			const args = Object.assign(params, {
				success: res=>{
					resolve(res);
				},
				fail: error=>{
					reject(error);
				}
			});
			// 执行传入的函数，同时将处理好的参数传入函数内部执行
			func(args);
		})
	}
}

const formatTime = function(result){
		let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
		let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
		let s = Math.floor(result % 60) < 10 ? '0' + Math.floor(result % 60) : Math.floor(result % 60);
		return result > 3600 ? h + ":" + m + ":" + s : m + ":" + s; 
	}

const dateTime = function(time){
	let old = new Date(time)
	let now = new Date()
	
	// 获取具体的old时间
	let d = old.getTime()
	let h = old.getHours()
	let m = old.getMinutes()
	let Y = old.getFullYear()
	let M = old.getMonth()
	let D = old.getDate()
	
	// 获取具体的old时间
	let nd = now.getTime()
	let nh = now.getHours()
	let nm = now.getMinutes()
	let nY = now.getFullYear()
	let nM = now.getMonth()
	let nD = now.getDate()
	if(h < 10)
		h = '0' + h;
	if(m < 10)
		m = '0' + m;
	
	// 当天时间
	if(Y === nY && M === nM && D === nD ){
		return h + ':' + m
	}
	
	// 昨天
	if(Y === nY && M === nM && D + 1 === nD ){
		return '昨天 ' +  h + ':' + m
	}
	
	// 前天
	if(Y === nY && M === nM && D + 2 === nD ){
		return '前天 ' +  h + ':' + m
	}
	
	// 大于两天
	if(Y === nY && M === nM && D + 2 < nD ){
		if(M < 10)
			M = '0' + M;
		if(D < 10)
			D = '0' + D;
		return M + '-' + D + " " +  h + ':' + m
	}
	
	// 不同月份
	if(Y === nY && M !== nM){
		if(M < 10)
			M = '0' + M;
		if(D < 10)
			D = '0' + D;
		return M + '-' + D + " " +  h + ':' + m
	}
	
	// 不同年份
	if(Y !== nY ){
		if(M < 10)
			M = '0' + M;
		if(D < 10)
			D = '0' + D;
		return Y + '-' + M + '-' + D + " " +  h + ':' + m
	}
}

const spaceTime = function(oldTime,nowTime){
	let old = new Date(oldTime)
	let now = new Date(nowTime)
	let told = old.getTime()
	let tnow = now.getTime()
	if(Math.abs(told - tnow) > 1000 * 60 * 5){
		return now;
	}
	return ''
}

const setStorage = function(key, value){
	try {
	    uni.setStorageSync(key, value);
	} catch (e) {
	    // error
		console.log(e)
	}
}

const getStorage = function(key){
	try {
	    const value = uni.getStorageSync(key);
	    if (value) {
	       return value;
	    }
	} catch (e) {
	    // error
		console.log(e)
	}
}

const removeStorage = function(key, value){
	try {
	    uni.removeStorageSync(key);
	} catch (e) {
	    // error
		console.log(e)
	}
}

const clearStorage = function(key, value){
	try {
	    uni.clearStorageSync();
	} catch (e) {
	    // error
		console.log(e)
	}
}

const getAstro = function(m,d){    

    return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m*2-(d<"102223444433".charAt(m-1)- -19)*2,2);
}

const utils = {
	promisic : promisic,
	formatTime : formatTime,
	dateTime : dateTime,
	spaceTime:spaceTime,
	setStorage,
	getStorage,
	removeStorage,
	clearStorage,
	getAstro
}

export  {
	utils,
	promisic,
};